/*
 * Copyright (C) 2011-2018 ShenZhen iBOXSaaS Information Technology Co.,Ltd.
 * 
 * All right reserved.
 * 
 * This software is the confidential and proprietary information of iBOXSaaS Company of China.
 * ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the contract agreement you entered into with iBOXSaaS inc.
 * 
 */

package com.iboxpay.open.auth.center.validate.impl;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.iboxpay.open.common.constants.ParamEnums;
import org.iboxpay.open.common.constants.ResultEnums;
import org.iboxpay.open.common.exception.ParamValidateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iboxpay.open.auth.center.basedata.BaseData;
import com.iboxpay.open.auth.center.validate.ParamValidate;
import com.iboxpay.open.common.db.entity.AppBaseInfo;

/**
 * 
 * AppIdValidate.java
 * @author xuxiao
 * @date 2018/06/25
 */
@Service
public class AppIdValidate implements ParamValidate {

    @Autowired
    private BaseData baseData;

    @Override
    public void validate(Map<String, Object> requestMap) {
        String appIdStr = requestMap.get(ParamEnums.appId.getCode()) == null ? "" : (String) requestMap.get(ParamEnums.appId.getCode());
        if (StringUtils.isBlank(appIdStr)) {
            throw new ParamValidateException(ResultEnums.appIdIncorrect);
        }
        Long appId = null;
        try {
            appId = Long.valueOf(appIdStr);
        } catch (Exception e) {
            throw new ParamValidateException(ResultEnums.appIdIncorrect);
        }
        AppBaseInfo appBaesInfo = baseData.appBaseInfoMap.get(appId);
        if(appBaesInfo == null) {
            throw new ParamValidateException(ResultEnums.appIdIncorrect);
        }
        // TODO 状态类更新
        if(appBaesInfo.getAppStatus() != 2) {
            throw new ParamValidateException(ResultEnums.appStatusUnusual);
        }
        if(StringUtils.isBlank(appBaesInfo.getRedirectUrl())) {
            throw new ParamValidateException(ResultEnums.appUnableAuth);
        }
    }

}
